package net.example.activity.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import net.example.model.SelectableItemModel;
import net.example.ggtalk.R;

import java.util.List;

public class SelectableItemAdapter extends BaseAdapter {

    private List<SelectableItemModel> modelList;
    private Context context;
    public SelectableItemAdapter(Context context, List<SelectableItemModel> modelList)
    {
        this.context=context;
        this.modelList=modelList;
    }

    @Override
    public int getCount() {
        return this.modelList.size();
    }

    @Override
    public Object getItem(int position) {
        return this.modelList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        try{
            SelectableItemAdapter.ViewHolder holder;
            if (view == null) {
                view = LayoutInflater.from(context).inflate(
                        R.layout.selectable_item, parent, false);
                holder = new SelectableItemAdapter.ViewHolder(view);
                view.setTag(holder);
            } else {
                holder = (SelectableItemAdapter.ViewHolder) view.getTag();
            }
            SelectableItemModel model = this.modelList.get(position);
            holder.key.setText(model.Key);
            if(model.Checked)
            {
                holder.checked.setVisibility(View.VISIBLE);
            }
            else {
                holder.checked.setVisibility(View.INVISIBLE);
            }
        }catch (Exception ex){ex.printStackTrace();}
        return view;
    }

    private class ViewHolder
    {
        public ImageView checked;
        public TextView key;

        public ViewHolder(View view)
        {
            this.checked= (ImageView) view.findViewById(R.id.checked_imageView);
            this.key=(TextView) view.findViewById(R.id.list_view_item_key);
        }
    }
}
